﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar;
using PlanningGIS.Util;
using System.Drawing;
using System.Windows.Forms;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 绘制空间对象工具条
    /// </summary>
    public partial class CtlDrawToolBar : ToolBar
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlDrawToolBar()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
            AddItems(); 
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="container">控件容器</param>
        public CtlDrawToolBar(IContainer container)
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            container.Add(this);
            InitializeComponent();
            AddItems();
        }

        /// <summary>
        /// 
        /// </summary>
        private bool _IsToMap = false;

        /// <summary>
        /// 是否添加到地图
        /// </summary>
        public bool IsToMap
        {
            get { return _IsToMap; }
            set
            {
                if (_IsToMap != value)
                {
                    _IsToMap = value;
                    if (this.Items.Count == 0) return;
                    foreach (BaseItem btn in this.Items)
                    {
                        if (btn.Tag != null && btn.Tag is IDrawGeometry)
                        {
                            (btn.Tag as IDrawGeometry).IsToMap = value;
                        }
                    }
                }
            }
        }
        
        /// <summary>
        /// 添加按钮项
        /// </summary>
        private void AddItems()
        {
            List<ValueName> btns = new List<ValueName>()
            {
                new ValueName(){Value="ToolDrawRectangle",Name="绘制矩形"},
                new ValueName(){Value="ToolDrawPolygon",Name="绘制多边形"},
                new ValueName(){Value="ToolDrawCircle",Name="绘制圆"},
                new ValueName(){Value="ToolDrawLine",Name="绘制线"},
                new ValueName(){Value="ToolDrawMarker",Name="绘制点"}
            };
            AddItems(btns, "ZJGIS.Engine.Controls");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Click(object sender, EventArgs e)
        {
            ButtonItem btn = (ButtonItem)sender;
            if (btn.Tag == null)
                btn.Tag = Activator.CreateInstance(Type.GetType("ZJGIS.Engine.Controls." + btn.Name + ",ZJGIS.Engine.Controls"));
            //object obj = Activator.CreateInstance(Type.GetType("ZJGIS.Engine.Controls." + btn.Name + ",ZJGIS.Engine.Controls"));
            CurrentCommand = btn.Tag as ESRI.ArcGIS.SystemUI.ICommand;
            SelectedChanged();
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void SelectedChanged()
        {
            if (Hook != null && CurrentCommand!=null)
            {
                CurrentCommand.OnCreate(Hook);
                CurrentCommand.OnClick();
            }
        }
    }
}
